package cn.edu.hzvtc.service;

import cn.edu.hzvtc.dao.EmployeeMapper;
import cn.edu.hzvtc.entity.Employee;
import cn.edu.hzvtc.entity.ReturnMsg;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    public List<Employee> getAll() {
        return employeeMapper.selectAllWithDept();
    }

    public Boolean del(String ids) {
        Boolean result = false;

        if (ids.contains("-")) {
            String[] str_ids = ids.split("-");
            List<Integer> delIds = new ArrayList<Integer>();
            for (String str_id : str_ids) {
                delIds.add(Integer.parseInt(str_id));
            }
            if (employeeMapper.deleteEmps(delIds) > 0) {
                result = true;
            }
        } else {
            Integer id = Integer.parseInt(ids);
            if (employeeMapper.deleteByPrimaryKey(id) > 0) {
                result = true;
            }
        }

        return result;
    }

    public Boolean save(Employee employee) {
        Boolean result = false;

        //必要的检测
        if(checkEmpName(employee.getEmpName())){
            if (employeeMapper.insert(employee) > 0) {
                result = true;
            }
        }
        return result;
    }

    public Boolean checkEmpName(String empName){
        Boolean result = false;
        Employee employee = new Employee();
        employee.setEmpName(empName);
        if (employeeMapper.selectByCondition(employee).size() == 0){
            result = true;
        }
        return result;
    }

    public Employee getEmp(Integer id){
        return employeeMapper.selectByPrimaryKey(id);
    }

    public void updateEmp(Employee employee){
        employeeMapper.updateByPrimaryKey(employee);
    }

    public void export(HttpServletResponse response) throws Exception{
        response.setCharacterEncoding("UTF-8");
        List<Employee> employees = getAll();
        //创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建sheet页
        HSSFSheet sheet = wb.createSheet("员工信息表");
        //创建标题行
        HSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("ID");
        titleRow.createCell(1).setCellValue("名字");
        titleRow.createCell(2).setCellValue("性别");
        titleRow.createCell(3).setCellValue("邮箱");
        titleRow.createCell(4).setCellValue("部门");

        //遍历将数据放到excel列中
        for (Employee emp : employees) {
            HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
            dataRow.createCell(0).setCellValue(emp.getEmpId());
            dataRow.createCell(1).setCellValue(emp.getEmpName());
            dataRow.createCell(2).setCellValue(emp.getEmpGender().equals("1") ? "男" : "女");
            dataRow.createCell(3).setCellValue(emp.getEmpEmail());
            dataRow.createCell(4).setCellValue(emp.getDepartment().getDeptName());

        }

        response.setContentType("application/octet-stream;charset=utf-8");
        response.setContentType("application/x-msdownload");
        response.setHeader("Content-Disposition", "attachment;filename="
                + new String("员工名单".getBytes(), "iso-8859-1") + System.currentTimeMillis() + ".xls");

        OutputStream ouputStream = response.getOutputStream();

        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
    }
}
